//
//  XCCollectionCellForRecommend.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCollectionCellForRecommend.h"
#import "HYModelForRecommend.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "HJCarouselViewLayout.h"
#import "NetworkingHandlerOfBlock.h"

#import "HYCollectionViewCellForT13.h"
#import "HYCollectionViewCellForT5.h"
#import "HYCollectionViewCellForT21.h"
#import "HYCollectionViewCellForT22.h"
#import "HYCollectionViewCellForT20.h"

@interface XCCollectionCellForRecommend () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionForRecommend;
@property (nonatomic, strong) HJCarouselViewLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrData;

@end

@implementation XCCollectionCellForRecommend


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createCollection];
        [self handleData];
    }
    
    return self;
}

- (void)handleData {
    
    self.arrData = [NSMutableArray array];
    
    NSString *str = @"http://api.dongting.com/frontpage/frontpage?uid=0&f=f62&v=v8.4.0.2016022715&utdid=VK4nCDeiX9UDAL%2B8TvJRNjEl&net=2&s=s200&location=1";
    
    [NetworkingHandler networkingWithUrl:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSArray *arr = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in arr) {
            
            HYModelForRecommend *model = [[HYModelForRecommend alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrData addObject:model];
            
        }
        
        [self.collectionForRecommend reloadData];
        
    }];
}

- (void)createCollection {
    
    self.layout = [[HJCarouselViewLayout alloc] initWithAnim:2];
    
    self.collectionForRecommend = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
    
    self.layout.itemSize = CGSizeMake(self.contentView.frame.size.width * 6 / 7, self.contentView.frame.size.height * 3 / 4);
    self.collectionForRecommend.backgroundColor = [UIColor whiteColor];
    
    self.collectionForRecommend.dataSource = self;
    self.collectionForRecommend.delegate = self;
    
    [self addSubview:self.collectionForRecommend];
    
    [self.collectionForRecommend registerClass:[HYCollectionViewCellForT13 class] forCellWithReuseIdentifier:@"PoolRecT13"];
    
    [self.collectionForRecommend registerClass:[HYCollectionViewCellForT5 class] forCellWithReuseIdentifier:@"PoolRecT5"];
    
    [self.collectionForRecommend registerClass:[HYCollectionViewCellForT21 class] forCellWithReuseIdentifier:@"PoolRecT21"];
    
    [self.collectionForRecommend registerClass:[HYCollectionViewCellForT22 class] forCellWithReuseIdentifier:@"PoolRecT22"];
    
    [self.collectionForRecommend registerClass:[HYCollectionViewCellForT20 class] forCellWithReuseIdentifier:@"PoolRecT20"];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.layout = nil;
    
    self.layout = [[HJCarouselViewLayout alloc] initWithAnim:HJCarouselAnimCarousel];
    
    self.collectionForRecommend.frame = self.contentView.frame;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HYModelForRecommend *model = [self.arrData objectAtIndex:indexPath.item];
    
    if ([[model.action objectForKey:@"type"] integerValue] == 13) {
        
        HYCollectionViewCellForT13 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT13" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        cell.layer.shadowColor = [[UIColor grayColor]CGColor];
        
        [cell passMoldel:model];
        
        return cell;
        
    } else if ([[model.action objectForKey:@"type"] integerValue] == 5) {
        
        HYCollectionViewCellForT5 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT5" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        
        [cell passMoldel:model];
        
        return cell;
        
    } else if ([[model.action objectForKey:@"type"] integerValue] == 21) {
        
        HYCollectionViewCellForT21 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT21" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        
        [cell passMoldel:model];
        
        return cell;
        
    } else if ([[model.action objectForKey:@"type"] integerValue] == 22) {
        
        HYCollectionViewCellForT22 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT22" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        
        [cell passMoldel:model];
        
        return cell;
        
    } else if ([[model.action objectForKey:@"type"] integerValue] == 20) {
        
        HYCollectionViewCellForT20 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT20" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        
        [cell passMoldel:model];
        
        return cell;
        
    } else {
        
        HYCollectionViewCellForT5 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolRecT5" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor whiteColor];
        cell.layer.shadowOffset = CGSizeMake(-5, -5);
        cell.layer.shadowOpacity = 1;
        
        [cell passMoldel:model];
        
        return cell;
    }
    
}

@end
